#!/usr/bin/env python3
# coding:utf-8
import os
import time

def get_io_stats(device_file):
    with open(device_file, 'r') as f:
        stats = f.read().split()
        read_ios = int(stats[0])
        write_ios = int(stats[4])
        read_sectors = int(stats[2])
        write_sectors = int(stats[6])
        return read_ios, write_ios, read_sectors, write_sectors

def get_disk_io_rate(device_name, interval=1):
    device_file =f'/sys/block/{device_name}/stat'
    read_ios_start, write_ios_start, read_sectors_start, write_sectors_start = get_io_stats(device_file)
    time.sleep(interval)
    read_ios_end, write_ios_end, read_sectors_end, write_sectors_end = get_io_stats(device_file)

    read_bytes = (read_sectors_end - read_sectors_start) * 512
    write_bytes = (write_sectors_end - write_sectors_start) * 512

    read_rate = (read_bytes / (1024 * 1024)) / interval
    write_rate = (write_bytes / (1024 * 1024)) / interval

    return read_rate, write_rate

if __name__ == '__main__':
    device_name = input("Enter the disk device name without /dev/ prefix (e.g., sda, sdb): ")
    while True:
        read_rate, write_rate = get_disk_io_rate(device_name)
        print(f"Read rate: {read_rate:.2f} MB/s, Write rate: {write_rate:.2f} MB/s")
        time.sleep(1)